<template>
	<view class="login-page">
		<!-- <uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
					title="自定义导航栏" @clickLeft="back" /> -->
		
		<button type="primary" open-type="chooseAvatar" @chooseavatar="handleGetAvatar">授权登录</button>
		<navigator class="access-login" open-type="navigate" url="/pages/accesslogin/accesslogin">用户密码登录</navigator>
		<img class="user-img" :src="userImg" alt="">
		<input type="nickname" class="weui-input" placeholder="请输入昵称"/>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import DO_LOGIN from '../../api/login';
	const userImg = ref('')
	const userName = ref('')
	const doLogin = () => {
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				console.log(loginRes);
				const {code} = loginRes
				console.log(code)
				//云函数
				// uniCloud.callFunction({
				// 	name: "login",
				// 	data: {
				// 		code: code
				// 	},
				// 	success(res) {
				// 		const {token} = res.result
				// 		uni.setStorageSync('token', token)
				// 		uni.switchTab({
				// 			url:"/pages/my/my"
				// 		})
				// 	}	
				// })
				// uni.request({
				// 	url:"https://env-00jxham84tk1.dev-hz.cloudbasefunction.cn/login",
				// 	method: "POST",
				// 	data:{
				// 		code
				// 	},
				// 	success(res) {
				// 		console.log(res)
				// 		const openId = res.data.data.openid
				// 		const sessionKey = res.data.data.session_key
				// 	}
				// })
				DO_LOGIN(code).then(res => {
					console.log('login.vue中输出')
					console.log(res)
					const {token, openid} = res
					uni.setStorageSync('token', token)
					uni.setStorageSync('openid', openid)
					console.log(token, openid)
					uni.setStorageSync('userInfo', {
						avatarUrl: userImg.value,
						nickName: '测试昵称'
					})
					uni.navigateTo({
						url:'/pages/index/index'
					})
				})
			}
		})
	}
	
	const handleGetAvatar = (res) => {
		console.log(res)
		userImg.value = res.detail.avatarUrl
		doLogin()
	}
</script>

<style lang="scss" scoped>
	.login-page{
		padding-top: 100rpx;
		width: 80%;
		margin: 0 auto;
	}
	.access-login{
		height: 100rpx;
		width: 100%;
		line-height: 100rpx;
		text-align: center;
	}
	.user-img{
		height: 50rpx;
		width: 50rpx;
	}
</style>